package com.suyang.syt.model.dto;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;

public class Captcha {
	/**
	 * 验证码图片宽度
	 */
	private int width;

	/**
	 * 验证码图片高度
	 */
	private int height;

	/**
	 * 验证码字数个数
	 */
	private int num;

	/**
	 * 验证码编码
	 */
	private String code;

	/**
	 * 随机数
	 */
	private static final Random ran = new Random();

	private static Captcha captcha;

	private Captcha() {
		code = "0123456789ABCDEFGHJKMNPRSTWXY";
		num = 4;
	}

	public static Captcha getInstance() {
		if (captcha == null) {
			captcha = new Captcha();
		}
		return captcha;
	}

	public void setCaptcha(int width, int height, int num, String code) {
		this.width = width;
		this.height = height;
		this.num = num;
		this.code = code;
	}

	public void setCaptcha(int width, int height) {
		this.width = width;
		this.height = height;
	}

	/**
	 * 获得验证码图片宽度
	 * 
	 * @return
	 */
	public int getWidth() {
		return width;
	}

	/**
	 * 设置验证码图片宽度
	 * 
	 * @param width
	 */
	public void setWidth(int width) {
		this.width = width;
	}

	/**
	 * 获得验证码图片高度
	 * 
	 * @return
	 */
	public int getHeight() {
		return height;
	}

	/**
	 * 设置验证码图片高度
	 * 
	 * @param height
	 */
	public void setHeight(int height) {
		this.height = height;
	}

	/**
	 * 获得验证码字数个数
	 * 
	 * @return
	 */
	public int getNum() {
		return num;
	}

	/**
	 * 设置验证码字数个数
	 * 
	 * @param num
	 */
	public void setNum(int num) {
		this.num = num;
	}

	/**
	 * 获得验证码编码
	 * 
	 * @return
	 */
	public String getCode() {
		return code;
	}

	/**
	 * 设置验证码编码
	 * 
	 * @param code
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * 生成验证码字符串
	 * 
	 * @return
	 */
	public String generateCheckcode() {
		StringBuilder cc = new StringBuilder();
		for (int i = 0; i < num; i++) {
			cc.append(code.charAt(ran.nextInt(code.length())));
		}
		return cc.toString();
	}

	/**
	 * 生成验证码图片
	 * 
	 * @param checkcode
	 * @return
	 */
	public BufferedImage generateCheckImg(String checkcode) {
		// 创建一个图片对象
		BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		// 获取图片对象的画笔
		Graphics2D graphic = img.createGraphics();
		graphic.setColor(Color.WHITE);
		graphic.fillRect(0, 0, width, height);
		graphic.setColor(Color.BLACK);
		graphic.drawRect(0, 0, width - 1, height - 1);
		Font font = new Font("宋体", Font.BOLD + Font.ITALIC, (int) (height * 0.8));
		graphic.setFont(font);
		for (int i = 0; i < num; i++) {
			graphic.setColor(new Color(ran.nextInt(255), ran.nextInt(255), ran.nextInt(255)));
			// graphic.setColor(new Color(0, 0, 0));
			graphic.drawString(String.valueOf(checkcode.charAt(i)), i * (width / num) + 4, (int) (height * 0.8));
		}

		// 加一些点
		for (int i = 0; i < (width + height); i++) {
			graphic.setColor(new Color(ran.nextInt(255), ran.nextInt(255), ran.nextInt(255)));
			graphic.drawOval(ran.nextInt(width), ran.nextInt(height), 1, 1);
		}

		// 加一些线
		for (int i = 0; i < 4; i++) {
			graphic.setColor(new Color(ran.nextInt(255), ran.nextInt(255), ran.nextInt(255)));
			graphic.drawLine(0, ran.nextInt(height), width, ran.nextInt(height));
		}
		return img;
	}

}
